package com.nashuju.web.util;

import java.util.ArrayList;
import java.util.List;

import com.nashuju.web.entity.sys.SysRes;


public class TreeUtil {

    public static List<SysRes> buildTreeData(List<SysRes> list) {

        List<SysRes> menu = new ArrayList<SysRes>();

        resolveMenuTree(list, 0, menu);

        return menu;
    }

    public static int resolveMenuTree(List<SysRes> menus, int parentMenuId, List<SysRes> nodes) {

        int count = 0;
        for (SysRes menu : menus) {
            if (menu.getParentId() == parentMenuId) {
                SysRes node = new SysRes();

                nodes.add(node);
                node.setSrId(menu.getId());
                node.setResName(menu.getText());
                node.setUrl(menu.getUrl());
                node.setParentId(menu.getParentId());
                node.setChildren(new ArrayList<SysRes>());
                node.setSysFuns(menu.getSysFuns());

                resolveMenuTree(menus, menu.getId(), node.getChildren());
                count++;
            }
        }
        return count;
    }
}
